var Path = require('path');
var Webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var ImageminPlugin = require('imagemin-webpack-plugin').default;
var ImageminMozjpeg = require('imagemin-mozjpeg');
var QiniuPlugin = require('qiniu-webpack-plugin');
var WebpackCleanupPlugin = require('webpack-cleanup-plugin')
// var OpenBrowserPlugin = require('open-browser-webpack-plugin');

var resolve = {
    extensions: ['', '.js', '.css', '.scss', '.png', '.jpg', '.ts', '.vue', '.html'],
}
var entry = {
    main: './src/ietest.ts'
}
var output = {
    path: './build',
    filename: 'js/[name]-[chunkhash:8].js',
    chunkFilename: 'js/[name]-[chunkhash:8].js'
}
var loaders = [
    { test: /\.vue$/, loader: 'vue-loader' },
    { test: /\.html$/, loader: "html-loader" },
    {
        test: /\.(png|gif|jpe?g)$/, loader: 'url-loader',
        query: {
            limit: 1,
            name: 'images/[hash:8].[ext]'
        }
    },
    {
        test: /\.(eot|woff|woff2|ttf|svg)$/,
        loader: 'url-loader',
        query: {
            limit: 1,
            name: '/font/[name]-[hash:8].[ext]'
        }
    },
    {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader!postcss-loader", {
            publicPath: '../'
        })
    },
    {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader!sass-loader", {
            publicPath: '../'
        })
    },
    {
        test: /\.ts$/,
        loader: 'ts-loader'
    }
];

var plugins = [
    /*
     * Search for equal or similar files and deduplicate them in the output
     * （删除重复依赖的文件）
     */
    new Webpack.optimize.DedupePlugin(),
    /*
     * Using this config the vendor chunk should not be changing its hash unless you change its code or dependencies
     * （避免在文件不改变的情况下hash值不变化）
     * */
    // new Webpack.optimize.OccurenceOrderPlugin(),
    new HtmlWebpackPlugin({
        template: 'src/ietest.html',
        inject: 'body',
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeAttributeQuotes: true
            // more options:
            // https://github.com/kangax/html-minifier#options-quick-reference
        },
        // necessary to consistently work with multiple chunks via CommonsChunkPlugin
        chunksSortMode: 'dependency'
    }),
    new ExtractTextPlugin("css/[name]-[chunkhash:8].css", { allChunks: true }),
    new Webpack.DefinePlugin({
        __SERVICES__: JSON.stringify(Path.resolve(__dirname, 'services')),
        __LIBS__: JSON.stringify(Path.resolve(__dirname, 'libs')),
        __VIEWS__: JSON.stringify(Path.resolve(__dirname, 'src', 'views')),
        __COMPONENTS__: JSON.stringify(Path.resolve(__dirname, 'src', 'components')),
        __MAIN__: JSON.stringify(Path.resolve(__dirname, 'src', 'main'))
    }),
    new ImageminPlugin({
        pngquant: {
            quality: 70
        },
        plugins: [
            ImageminMozjpeg({
                progressive: true
            })
        ]
    }),

    /***********************************************
    new Webpack.optimize.CommonsChunkPlugin(
        devServer ?
            { name: "common", filename: "js/common.js" } :
            { names: ["common", "webpackAssets"] }
    ),
    new Webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        "window.jQuery": "jquery",
        "avalon": "avalon2",
        "window.avalon": "avalon2"
    }),
    **********************************************/
];

switch (process.env.npm_lifecycle_event) {
    case "release":
        plugins.push(new WebpackCleanupPlugin())
        plugins.push(new Webpack.optimize.UglifyJsPlugin({
            compress: {
                screw_ie8: false,
                warnings: false
            },
            output: {
                screw_ie8: false,
                comments: false
            },
            mangle: true
        }))
        // plugins.push(new QiniuPlugin({
        //     ACCESS_KEY: 'iAeJF0UXDw2YplNbTHgNP9fSdjcgGzXQpXHTVWDo',
        //     SECRET_KEY: 'JC7MccZR5aiS70wUPxzx5O53XiUPiuwJgcDViGHv',
        //     bucket: 'shindousaigo',
        //     path: ''
        // }))
        // output.publicPath = "http://on56b65dl.bkt.clouddn.com/"
        break;
}

module.exports = {
    entry: entry,
    output: output,
    resolve: resolve,
    resolveLoader: { root: Path.join(__dirname, "node_modules") },
    plugins: plugins,
    module: {
        loaders: loaders
    },
    devServer: {
        contentBase: './src',
        port: 10080
    }
};








